package com.aaa.sbm.util;

import com.aaa.sbm.constants.ReturnStatus;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice  //统一处理异常，类初始化或者绑定属性  增强注解  所有的requestMapping都会经过
public class GlobalExceptionHandler {

    /**
     * 统一异常处理方法
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public Result handlerException(Exception ex){
        //处理自定义异常
        if(ex instanceof CustomException){
            CustomException customException = (CustomException) ex;
            return new Result<String>(customException.getErrorCode(),
                    customException.getErrorMsg());
        }
        //处理通用异常
        return new Result<String>(ReturnStatus.FAIL.getReturnCode(),
                ReturnStatus.FAIL.getReturnMsg(),
                ex.getClass().getName());
    }
}
